package com.zlm.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private DataSource dataSource;

    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
        userDetailsManager.createUser(User.withUsername("root").password("{noop}1234").roles("admin").build());
        return userDetailsManager;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .mvcMatchers("/index").rememberMe() // 只有这些地址才能实现rememberMe的操作
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .and()
                .rememberMe() // 开启记住我的功能
                // 底层默认开启了自定义的RememberService
                .tokenRepository(persistentTokenRepository())
                //.rememberMeServices(rememberMeServices()) // 指定自定义的rememberMeServices
                .and()
                .csrf().disable();
    }

    // 指定持久的数据源
    // 这种就是当你第一次启动你设置为true后面启动你要设置为false，因为表结构已经创建出来了
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        jdbcTokenRepository.setCreateTableOnStartup(false);
//        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }

//    下面这种方式就是说我们要手动去创建表后面就不用管了
//    @Bean
//    public RememberMeServices rememberMeServices() {
//        /*// 默认是基于内存的令牌
//        return new PersistentTokenBasedRememberMeServices(UUID.randomUUID().toString(), userDetailsService(),
//                new InMemoryTokenRepositoryImpl());*/
//
//        // 现在修改为基于数据库的
//        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
//        jdbcTokenRepository.setDataSource(dataSource);
////        jdbcTokenRepository.setCreateTableOnStartup(true); // 启动时候创建表结构
//        return new PersistentTokenBasedRememberMeServices(UUID.randomUUID().toString(), userDetailsService(),
//                jdbcTokenRepository);
//    }
}
